<script setup lang="ts">
import { inject, watch, ref } from 'vue'
import { Message, messageQueueKey } from '@/type'
const messageQueue = inject(messageQueueKey) as Message[]
import error from '../assets/error.svg'
import success from '../assets/success.svg'
import info from '../assets/info.svg'
const typeIcon = {
    error: error,
    success: success,
    warning: 'src/assets/warning.svg',
    info: info
}
// 计时器id
let id: any;
let hide = ref(false)
/**
 * 用于设置销毁弹窗
 */
watch(messageQueue, () => {
    if ((id == undefined || id == null) && messageQueue.length > 0) {
        id = setTimeout(() => {
            const message = messageQueue.shift()
            if (message?.callback != undefined) {
                message.callback()
            }
            id = null;
            hide.value = false;
        }, 2000)
        setTimeout(() => {
            hide.value = true
        }, 1500)
    }
})
</script>
<template>
    <div class="messageList">
        <template v-for="message of messageQueue">
            <div class="dialog showPop" :class="{ hidePop: hide, [message.type]: true }">
                <img class="icon" :src="typeIcon[message.type]" alt="">
                <span class="message">{{ message.message }}</span>
            </div>
        </template>
    </div>
</template>
<style lang="css" scoped>
@keyframes show {
    0% {
        opacity: 0;
        transform: translateY(-50%);
    }

    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes hide {
    0% {
        opacity: 1;
        transform: translateY(0);
    }

    100% {
        opacity: 0;
        transform: translateY(-50%);
    }
}

.hidePop:first-child {
    animation: hide 0.5s;
}

.showPop {
    animation: show 0.5s;
}

.message {
    margin-left: 10px;
}

.icon {
    width: 15px;
    height: 15px;
}

.success {
    background-color: #1c2518;
    color: #67c23A;
}

.error {
    background-color: #2B1D1D;
    color: #f56c6c
}

.info {
    background-color: #d9edf7;
    color: #31708f;
    /* background-color: #f2dede;
    color: #a94442; */

}

.messageList {
    z-index: 99999;
    position: fixed;
    left: 50%;
    transform: translate(-50%, 0);
}

.dialog {
    display: flex;
    justify-items: center;
    align-items: center;
    height: 50px;
    margin-top: 10px;
    padding: 0 25px;
    border-radius: 4px;
}
</style>